package main

import (
	"app/internal/web/conf"
	"app/internal/web/http"
	"app/internal/web/service"
	"app/util"
	"flag"
	"os"
	"runtime"
)

var (
	confpath string
)

func main() {

	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.StringVar(&confpath, "conf", "", "default config path")
	flag.Parse()

	cf := conf.LoadConfig(confpath)
	service.InitService(&cf)

	ms := util.NewMessageloop(3)

	web := http.NewHttpEngine()

	go func() {
		defer ms.Signal()
		if err := web.Init(&cf); err != nil {
			ms.Exit()
		}
		if err := web.Run(); err != nil {
			util.Error("Run End")
			ms.Exit()
		}
	}()
	go func() {
		defer ms.Signal()
		handler := func(s os.Signal, arg interface{}) {
			srv := arg.(*http.HttpEngine)
			srv.HttpClose()
			service.GetServiceInstance().Close()
			ms.Exit()
		}
		util.WaitSignal(handler, web)
	}()
	go func() {
		defer ms.Signal()
		util.ProcessNotify(cf.Monitor.GetMonitorAddr())
	}()

	ms.Wait()

}
